package com.wee.fragment.find;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;

import com.bigtotoro.util.SharedPreferencesUtil;
import com.bigtotoro.widgets.AppProgressBar;
import com.google.gson.Gson;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.wee.adapter.CourseAdapter1;
import com.wee.aircoach_user.CoachPlanActivity;
import com.wee.aircoach_user.Constant;
import com.wee.aircoach_user.MyApplication;
import com.wee.aircoach_user.R;
import com.wee.entity.Coach_project;
import com.wee.entity.Coach_projectResponse;
import com.wee.entity.MD5Util;

import org.apache.http.Header;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by STS on 2016/4/5.
 */
public class CourseActivity extends Activity{
    private ListView systemCoachList;
    private CourseAdapter1 courseAdapter;
    private ImageView back;
    private List<Coach_project> projects = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.faxian_fragment2);
        initview();

    }
    private void initview() {
        systemCoachList = (ListView)findViewById(R.id.list_kecheng);
        courseAdapter = new CourseAdapter1(CourseActivity.this, projects);
        back = (ImageView) findViewById(R.id.back);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();

            }
        });
        systemCoachList.setAdapter(courseAdapter);
        systemCoachList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                Intent intent = new Intent(CourseActivity.this, CoachPlanActivity.class);
                intent.putExtra("CoachDown", projects.get(position).getId() + "");
                intent.putExtra("teacherId", projects.get(position).getCoach_id() + "");

                startActivity(intent);
            }
        });
        loadSystemCoac();

    }
    private void loadSystemCoac(){
        int exact=  SharedPreferencesUtil.getInt(CourseActivity.this, Constant.USERTIME);
        RequestParams params = new RequestParams();
        AppProgressBar.checkAndCreateProgressBar(CourseActivity.this);
        MyApplication.getInstance().getHttpClient().get(Constant.COACH_PROJECT + MD5Util.md5(exact), params, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int i, Header[] headers, byte[] bytes) {
                try {
                    Coach_projectResponse response = new Gson().fromJson(new String(bytes), Coach_projectResponse.class);
                    if (response != null && response.getData().size() != 0) {
                        projects.clear();
                        projects.addAll(response.getData());
                        courseAdapter.notifyDataSetChanged();
                    }
                    AppProgressBar.closeProgressBar();
                } catch (Exception r) {
                    Toast.makeText(CourseActivity.this, "网络不稳定", Toast.LENGTH_SHORT).show();
                    AppProgressBar.closeProgressBar();
                }
            }

            @Override
            public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {
                throwable.printStackTrace();
                AppProgressBar.closeProgressBar();
            }
        });
    }
}
